package com.mars.module.utils;

import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.Util;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class GetFontUtil {
    //根据上面的填写
    private static String APP_ID = "60211561";
    private static String API_KEY = "s13ibEIK61qxw9TVM2us3AXo";
    private static String SECRET_KEY = "bNP34G2btyWf2PqOdE9AaXZ0x6H6T3EI";

    public static String getImgString(String imgPath, String type) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        // 是否检测朝向
        options.put("detect_direction", "false");
        // 是否检测风险
        options.put("detect_risk", "false");

        // 正反面front /back
        String idCardSide = "front";

        // 参数为本地图片二进制数组
        byte[] file = new byte[0];
        try {
            file = Util.readFileByBytes(imgPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject res = new JSONObject();
        //这个可以根据不同需求引用不同方法
        if ("1".equals(type)) {
            //通用文字识别（高精度版） 参数为本地图片二进制数组
            res = client.basicAccurateGeneral(file, options);
        } else if ("2".equals(type)) {
            //手写文字识别
            res = client.handwriting(file, options);
        }
        JSONArray wordsResult = res.getJSONArray("words_result");
        String result = "";
        for (Object object : wordsResult) {
            JSONObject jsonObject = (JSONObject) object;
            for (String key : jsonObject.keySet()) {
                if ("words".equals(key)) {
                    System.out.println(key + ": " + jsonObject.get(key));
                    result += jsonObject.get(key);
                }
            }
        }
        return result;
    }
    public  void downloadFile(String remoteFilePath, String localFilePath) {
        URL website = null;
        ReadableByteChannel rbc = null;
        FileOutputStream fos = null;
        try {
            website = new URL(remoteFilePath);
            rbc = Channels.newChannel(website.openStream());
            fos = new FileOutputStream(localFilePath);//本地要存储的文件地址 例如：test.txt
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(rbc!=null){
                try {
                    rbc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
    public static void main(String[] args) {
        getImgString("/Users/caijin/Desktop/3.png", "1");
        //getImgString("/Users/caijin/Downloads/1.jpg","2");
    }
}
